(function(){
	'use strict';
	angular.module('controllers.workOrderData.indexMgmt')
	.controller('ChangeMgmtController',ChangeMgmtController);
	/**
	 * 衡量指标
	 * h1001：变更来源
	 * h1002：变更实施结果
	 * h1003：变更影响度
	 * h1004：变更紧急度
	 * h1005：风险等级
	 * h1006：变更成功率
	 */
	function ChangeMgmtController($scope,workOrderDataService,changeMgmtFactory,CoreLoading,PublicFn,$timeout,COREDATA){
		//初始化查询衡量指标
		$scope.initQuery = function(){
			workOrderDataService.queryMeasureIndex({code:'1'},function(res){
				$scope.measureIndexs = res.data;
				$scope.search();
			});
		};

		$scope.flag = {
			state: true,
			show: 'view',
			arrow: true,
			showPie: false
		};
		//默认时间为天
		$scope.sendData = workOrderDataService.getSendData('1');
		$scope.sendData.measureIndexCode = 'h1001';

		//导出路径
		$scope.formPrefix = COREDATA.FORMPREFIX+'workOrderData/changeMgmt/export';

		var myEcharts = echarts.init(document.getElementById('doc'));
		var myEcharts1 = echarts.init(document.getElementById('doc1'));
		var myEcharts2 = echarts.init(document.getElementById('doc2'));
		$scope.search = function(){
			$scope.flag.state = !$scope.flag.state;
			var xAxisData = [];  //echarts图x轴数据
			var seriesData = [];  //echarts图series数据
			var option = {};
			var option1 = {};
			var option2 = {};
			var measureIndexCode,legendName,timeTitle,name,obj,changeTypeCode,changeTypeName;
			if(!$scope.flag.state){
				obj = workOrderDataService.fmtSendData($scope.sendData);
				CoreLoading.setState(true);
				changeMgmtFactory.query(obj,function(res){
					if(res.state === '10000' && res.data.length >=1){
						myEcharts.clear();
						myEcharts1.clear();
						myEcharts2.clear();
						$scope.send = obj;
						timeTitle = PublicFn.fmtDateTitle($scope.send,'timeType');
						measureIndexCode = $scope.send.measureIndexCode;
						changeTypeCode = $scope.send.changeTypeCode;
						name = workOrderDataService.getName(measureIndexCode,$scope.measureIndexs,'measureIndexCode','measureIndexName');
						if(measureIndexCode === 'h1006'){
							changeTypeCode === '1' ? changeTypeName = '各类型' : changeTypeName = '各系统';
							$scope.send.title = timeTitle + changeTypeName + name +'统计';
						}else {
							//标题:时间+衡量指标+数量统计;
							$scope.send.title = timeTitle + name + '数量统计';
						}
						angular.forEach(res.data,function(item){
							if(item.showOrHide === 'show' && measureIndexCode !== 'h1006'){
								xAxisData.push(item.filedName);
								seriesData.push(Number(item.filedNum.replace('个','')));
							}else if(measureIndexCode ==='h1006'){
								xAxisData.push(item.changeTypeName);
								seriesData.push(Number(item.filedNum.replace('%','')));
							}
						});
						measureIndexCode === 'h1006' ? $scope.showSR = true : $scope.showSR = false;
						$scope.items = res.data;
						//当衡量指标为变更来源和风险等级或者变更成功率时
						if(measureIndexCode === 'h1001' || measureIndexCode === 'h1005' || measureIndexCode === 'h1006'){
							$scope.flag.showPie = false;
							if(measureIndexCode === 'h1006'){
								legendName = '变更成功率';
							}else{
								legendName = '变更数量';
							}
							option = workOrderDataService.getEchartsData(xAxisData,seriesData,$scope.send.title,legendName);
							if(measureIndexCode === 'h1006'){
								option.tooltip.formatter = workOrderDataService.getFormatterFn('%');
								option.yAxis[0] = workOrderDataService.formatterYAxis('','{value}%');
							}else {
								option.tooltip.formatter = workOrderDataService.getFormatterFn('个');
								option.yAxis[0] = workOrderDataService.formatterYAxis('单位:(个)','');
							}
							myEcharts.setOption(option);
							$scope.outData = workOrderDataService.exportView(myEcharts,$scope.send);
						}else{//当衡量指标为变更实施结果,变更影响度,变更紧急度
							$scope.flag.showPie = true;
							$scope.pieTitle = timeTitle + name + '占比';
							option1 = workOrderDataService.getEchartsData(xAxisData,seriesData,$scope.send.title,'变更数量');
							option2 = workOrderDataService.getEchartsPieData(res.dataView,$scope.pieTitle);
							option1.tooltip.formatter = workOrderDataService.getFormatterFn('个');
							option1.yAxis[0] = workOrderDataService.formatterYAxis('单位:(个)','');
							myEcharts1.setOption(option1);
							myEcharts2.setOption(option2);
							$scope.outData = changeMgmtFactory.exportView(myEcharts1,myEcharts2,$scope.send);
						}
						$scope.flag.show = 'view';
						$scope.flag.arrow = true;
					}else if(res.data.length === 0){
						workOrderDataService.queryNoDataOrError($scope.flag,'查询无数据');
					}else {
						workOrderDataService.queryNoDataOrError($scope.flag,'查询错误');
					}
					CoreLoading.setState(false);
				});
			}
		};

		$scope.changeMeasureIndex = function(code){
			code === 'h1006' ? $scope.sendData.changeTypeCode = '1' : $scope.sendData.changeTypeCode = '';
		};
		//监控页面时间，如果时间出错，则查询按钮禁止
		$scope.$watch('myForm.$invalid', function(newValue) {
			newValue ? $scope.flag.arrow = false : $scope.flag.arrow = true;
		});
	}
	ChangeMgmtController.$inject = ['$scope','workOrderDataService','changeMgmtFactory','CoreLoading','PublicFn','$timeout','COREDATA'];
})();